<?php

namespace App\Models;

class Login extends Base
{
    protected $table = 'logins';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">异常</span>',
    ];

    protected $fillable = [
        'id', 'aid', 'username', 'status', 'ip', 'address', 'login_at'
    ];

    public $timestamps = false;

    public function admin()
    {
        return $this->belongsTo('App\Models\Admin', 'aid', 'id');
    }

    protected function list($params = [], $fields = ['*'])
    {
        return $this->select($fields)
            ->where(function ($query) use ($params) {
                if (isset($params['kwd']) && $params['kwd']) {
                    $query->where('ip', 'like', '%' . $params['kwd'] . '%')
                        ->orWhere('username', 'like', '%' . $params['kwd'] . '%')
                        ->orWhere('address', 'like', '%' . $params['kwd'] . '%');
                }
                if (isset($params['status']) && $params['status']) {
                    $query->where('status', $params['status']);
                }
            })->with([
                'admin' => function ($query) use ($params) {
                    $query->select('id', 'nickname');
                }
            ])->orderBy('login_at', 'desc')
            ->paginate(config('setting.limit'));
    }


}
